// 挂号操作
<template>
  <div>
    <TopManu @handTohospital="handTohospital"></TopManu>
    <div class="boxs">
      <el-container>
        <el-container>
          <el-aside width="25%" style="border: 1px solid #f3f3f3; height: 85vh">
            <div class="leftTitle">
              <ViewTitle text="候诊患者" />
              <div class="leftTitle_but">
                <el-button
                  size="mini"
                  class="butB_Add"
                  icon="el-icon-phone"
                  @click="jiaohao()"
                  >叫号</el-button
                >
              </div>
            </div>
            <el-tabs v-model="tabName" type="card" @tab-click="handleClick">
              <el-tab-pane label="候诊患者" name="first">
                <leftSearch
                  ref="leftSearch"
                  @setrow="setrow(arguments)"
                  :leftParams="leftParams"
                  :tableData="tableData"
                  :tableTitle="tableTitle"
                  @getList="getList()"
                  :thatF="this"
                  :loading="loading"
                ></leftSearch>
              </el-tab-pane>
              <el-tab-pane label="接诊中" name="second">
                <leftSearch
                  ref="leftSearch"
                  @setrow="setrow(arguments)"
                  :leftParams="leftParams"
                  :tableData="tableData"
                  :tableTitle="tableTitle"
                  @getList="getList()"
                  :loading="loading"
                  :thatF="this"
                ></leftSearch>
              </el-tab-pane>
              <el-tab-pane label="已诊患者" name="third">
                <leftSearch
                  ref="leftSearch"
                  @setrow="setrow(arguments)"
                  :leftParams="leftParams"
                  :tableData="tableData"
                  :tableTitle="tableTitle"
                  @getList="getList()"
                  :loading="loading"
                  :thatF="this"
                ></leftSearch>
              </el-tab-pane>
            </el-tabs>

            <div>
              <el-pagination
                class="fenye"
                :background="true"
                :current-page="leftParams.page"
                :page-size="leftParams.limit"
                layout="prev, pager, next"
                @current-change="handlePagination"
                :total="pageCount"
              >
              </el-pagination>
            </div>
          </el-aside>
          <el-main style="border: 1px solid #f3f3f3; height: 85vh">
            <div class="rightCZ" v-if="PatientInfoData && tabName == 'second'">
              <rightCZ
                :selectParams="selectParams"
                ref="rightCZ"
                :thatF="this"
                :selectData="selectData"
                @getDiseaseDict="getDiseaseDict()"
                @getDrugItem="getDrugItem()"
                @getServiceItem="getServiceItem()"
              ></rightCZ>
            </div>
            <div
              class="rightYZ"
              :thatF="this"
              v-else-if="PatientInfoDatathird && tabName == 'third'"
            >
              <rightYZ ref="rightYZ" :thatF="this"></rightYZ>
            </div>
            <div v-else style="color: red">暂无患者信息...</div>
          </el-main>
        </el-container>
      </el-container>
    </div>

    <!-- 叫号弹框 -->
    <div class="jiaohao">
      <el-dialog :visible.sync="jiaohaoDialog" width="30%" center>
        <div class="jiaohaoDialogSty">
          <div>正在呼叫....</div>
          <div class="name">
            <span>门诊号：{{ jiaohaoData.patient_no }}</span>
            <span style="margin-left: 50px"
              >姓名：{{ jiaohaoData.member_info.name }}</span
            >
          </div>
          <div>卡号：{{ jiaohaoData.member_info.id_card }}</div>
        </div>
        <span slot="footer" class="dialog-footer">
             <el-button type="success" size="mini" @click="jiaohaoStatus(3)"
            >重呼</el-button
          >
          <el-button type="success" size="mini" @click="jiaohaoStatus(1)"
            >已到</el-button
          >
      
          <el-button type="success" size="mini" @click="jiaohaoStatus(2)"
            >下一位</el-button
          >
        </span>
      </el-dialog>
    </div>

    <div class="update">
      <el-dialog
        v-if="updateChufangDialog"
        :visible.sync="updateChufangDialog"
        :title="YZ_chufang_status=='add'?'添加':'修改'"
        :modal-append-to-body="false"
        destroy-on-close
      >
        <updateChufang :thatF="this" ref="updateChufang"></updateChufang>
      </el-dialog>
    </div>

    <!-- <el-dialog :fullscreen="true" :visible.sync="CreateDialog" title="转住院">
      <Create
        ref="create"
        :userdata="userData"
        @hospitalization="hospitalization(arguments)"
      ></Create>
      <span slot="footer" class="dialog-footer">
        <el-button @click="CreateDialog = false">取 消</el-button>
        <el-button type="primary" @click="handlization">保 存</el-button>
      </span>
    </el-dialog> -->
  </div>
</template>

<script>
let that;
import ViewTitle from "@/forComponents/viewTitle/index.vue";
import updateChufang from "./common/updateChufang.vue";
import rightCZ from "./common/rightCZ.vue";
import rightYZ from "./common/rightYZ.vue";
import leftSearch from "./common/leftSearch.vue";
import TopManu from "@/components/TopMenu";
import Create from "../../workstation/hospitalizedCharge/components/create.vue";
import { parse } from "path-to-regexp";
import { hospitalization } from "@/api/register";
import { transferHospital } from "@/api/zhuyanre";
import { mapState, mapMutations, mapActions } from "vuex";

// import $baseBai from "@/utils/baseBai"
export default {
  name: "DoctorWorkStation",
  data() {
    return {
      order_tyte: "",
      tabName: "first",
      tableTitle: this.$baseBai["workstation"]["leftTable1"],
      loading: false,
      tableData: [],
      leftParams: {
        initStart_time: "",
        keyword: "",
        start_time: "",
        leftRadio: 1,
        page: 1,
        limit: 10,
      },
      pageCount: 0,
      userData: {
        member: {},
        masterInfo: {},
        nodeInfo: { patient_no: "" },
      },
      jiaohaoDialog: false, //叫号弹框
      CreateDialog: false,
      jiaohaoData: {
        member_info: {},
      },

      selectParams: {
        keyword: "",
        page: 1,
        limit: 15,
        pageCount: 0,
      },
      selectData: {
        selectData1: [],
        selectData2: [],
        selectData3: [],
      },

      disu: [],
      pinci: [],
      geiyao: [],
      xiyaojiaozhu: [],
      dose_unit: [],

      PatientInfoData: "",
      PatientInfoDatathird: "",
      chufangData1: [],
      chufangData2: [],
      updateChufangDialog: false,
      updateChuFangData: {},
      YZ_chufang_info_loading: false,
      threeSelectLoading: false,
    };
  },

  mounted() {
    this.getList();
    this.getDiseaseDict();
    Promise.all([
      this.getDict_dataDictIndex("47,12,60"),
      this.getCommonGetPinci(),
      this.getDrug_channelGetList(),
      this.getDict_dataDictIndex(12),
      this.getDict_dataDictIndex(12),
    ]).then((res) => {
      let dict = {};
      for (let item of res[0].data) {
        dict[item.id] = item;
      }
      this.disu = dict[47].items;

      this.pinci = res[1].data;
      this.geiyao = res[2].data.list;
      this.xiyaojiaozhu = dict[12].items;
      this.dose_unit = dict[60].items;
      console.log("thatF.xiyaojiaozhu", this.xiyaojiaozhu);
    });
  },
  computed: {
    ...mapState({
      huixian_detail: (state) => state.doctorWorkStation.huixian_detail,
       YZ_chufang_status: (state) => state.doctorWorkStation.YZ_chufang_status,
    }),
  },
  methods: {
    ...mapMutations(["doctorWorkStation/set_patientInfo"]),
    ...mapActions(["doctorWorkStation/getClinic_doctorGetMedicalRecordDetail"]),

    handTohospital(type) {
      if (type == 2) {
        return this.$router.push({ name: "mz_log" });
      } else if (type == 3) {
        return this.$router.push({ name: "mz_statistics" });
      }
      if (this.userData.nodeInfo.patient_no != "") {
        this.$confirm("是否转住院", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
        }).then(() => {
          transferHospital({
            patient_no: this.userData.nodeInfo.patient_no,
          }).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: "操作成功",
                type: "success",
              });
            } else {
              //  this.$message(res.message);
            }
          });
        });
      } else {
        this.$message({
          message: "请选择患者",
        });
      }
    },
    setrow(arr) {
      this.userData.member = {};
      this.userData.member = arr[0].member_info;

      this.userData.nodeInfo.patient_no = arr[0].patient_no;
      if (this.tabName == "second") {
        //进入到接诊中的时候
        let d = {
          patient_no: arr[0].patient_no,
        };
        this["doctorWorkStation/getClinic_doctorGetMedicalRecordDetail"](
          d
        ).then((res) => {
          console.log("huixian_detail", this.huixian_detail);
        });
        this["doctorWorkStation/set_patientInfo"](arr[0]);
      }
    },
    // handlization() {
    //   this.$refs.create.hospitalization();
    // },
    // hospitalization(arr) {
    //   hospitalization(arr[0]).then((res) => {
    //     if (res.code == 200) {
    //       this.$message({
    //         message: "办理成功",
    //         type: "success",
    //       });
    //     }

    //     this.CreateDialog = false;
    //   });
    // },

    getDict_dataDictIndex(num) {
      let t = this;
      return new Promise((resolve, reject) => {
        t["_API"].getDict_dataDictIndex({ group_id: num }).then((res) => {
          resolve(res);
        });
      });
    },
    getCommonGetPinci() {
      let t = this;
      return new Promise((resolve, reject) => {
        t["_API"].getCommonGetPinci().then((res) => {
          resolve(res);
        });
      });
    },
    getDrug_channelGetList() {
      let t = this;
      return new Promise((resolve, reject) => {
        t["_API"].getDrug_channelGetList().then((res) => {
          resolve(res);
        });
      });
    },
    getDiseaseDict() {
      let d = {
        keyword: this.selectParams.keyword,
      };

      this["_API"].getClinic_doctorgetDiseaseDict(d).then((res) => {
        if (res.code == 200) {
          this.selectData.selectData1 = res.data.lists;
          this.threeSelectLoading = false;
        }
      });
    },
    getDrugItem() {
      let d = {
        keyword: this.selectParams.keyword,
        page: this.selectParams.page,
        limit: this.selectParams.limit,
      };

      return new Promise((resolve, reject) => {
        this["_API"].getClinic_doctorgetDrugItem(d).then((res) => {
          if (res.code == 200) {
            this.selectData.selectData2 = res.data.list;

            this.selectData.selectData2.forEach((res) => {
              if (res.is_skin_test === 1) {
                res.is_skin_test = true;
              } else {
                res.is_skin_test = false;
              }
            });
            this.selectParams.pageCount = res.data.count;
            this.threeSelectLoading = false;
            resolve(res);
          } else {
            reject(res);
          }
        });
      });
    },
    getServiceItem() {
      let d = {
        keyword: this.selectParams.keyword,
        page: this.selectParams.page,
        limit: this.selectParams.limit,
      };
      return new Promise((resolve, reject) => {
        this["_API"].getClinic_doctorgetServiceItem(d).then((res) => {
          if (res.code == 200) {
            this.selectData.selectData3 = res.data.list;
            this.selectParams.pageCount = res.data.count;
            this.threeSelectLoading = false;
            resolve(res);
          } else {
            reject(RES);
          }
        });
      });
    },
    jiaohaoStatus(num) {
      if (num == 3) {
        return this.jiaohao();
      }
      let d = {
        register_id: this.jiaohaoData.id,
        status: num,
      };

      this["_API"].getClinic_doctorCall(d).then((res) => {
        if (res.code == 200) {
          if (num == 1) {
            this.tabName = "second";
            this.handleClick();
            this.jiaohaoDialog = false;
          } else if (num == 2) {
            this.jiaohao();
            this.getList();
          }
        }
      });
    },
    jiaohao() {
      //  /clinic_doctor/getNextNumber

      this["_API"].getClinic_doctorgetNextNumber().then((res) => {
        console.log(res);
        if (res.code == 200) {
          this.jiaohaoDialog = true;
          this.jiaohaoData = res.data;
          var data_t={
              ipt_no:this.jiaohaoData.patient_no,
              name:this.jiaohaoData.member_info.name
          }
               this["_API"].callMember(data_t).then((res) => {
        console.log(res);
        if (res.code == 200) {
          this.$message({
                message: "呼叫成功",
                type: "success",
              });
          
        }
      });
        }
      });

      //jiaohaoDialog=true
    },
    getList(param) {
      this.loading = true;
      let status = 1;
      if (this.tabName == "first") {
        status = 1;
      } else if (this.tabName == "second") {
        status = 2;
      } else if (this.tabName == "third") {
        status = 3;
      }
      let d = {
        patient_no: "",
        member_name: "",
        id_card: "",
        start_time: this.leftParams.start_time,

        status: status,
        keyword: this.leftParams.keyword,
        page: this.leftParams.page,
        limit: this.leftParams.limit,
      };

      if (this.leftParams.leftRadio == 1) {
        d.register_doctor = 1;
      } else if (this.leftParams.leftRadio == 2) {
        d.register_dept = 1;
      }
      this["_API"].getClinic_doctorRegister_index(d).then((res) => {
        this.loading = false;
        if (res.code == 200) {
          this.tableData = res.data.list;
          this.pageCount = res.data.count;

          if (param == "init") {
            if (this.tabName == "second") {
              if (res.data.list.length > 0) {
                this.PatientInfoData = res.data.list[0];
                this["doctorWorkStation/set_patientInfo"](res.data.list[0]);

                this[
                  "doctorWorkStation/getClinic_doctorGetMedicalRecordDetail"
                ]({ patient_no: this.PatientInfoData.patient_no });
              } else {
                this.PatientInfoData = "";
              }
            } else if (this.tabName == "third") {
              if (res.data.list.length > 0) {
                this.PatientInfoDatathird = res.data.list[0];
                this.getClinic_doctorGetRecipe(res.data.list[0].patient_no);
              } else {
                this.PatientInfoDatathird = "";
              }
            }
          }
        }
      });
    },
    handlePagination(e) {
      this.leftParams.page = e;
      this.getList();
    },
    handleClick(e) {
      let data = this.$baseBai["workstation"];

      this.leftParams.page = 1;

      if (this.tabName == "first") {
        this.tableTitle = data["leftTable1"];
      } else if (this.tabName == "second") {
        this.tableTitle = data["leftTable1"];
      } else if (this.tabName == "third") {
        this.tableTitle = data["leftTable2"];
      }
      this.getList("init");
    },
    getClinic_doctorGetRecipe(patient_no) {
      let d = { patient_no: patient_no };
      this["_API"].getClinic_doctorGetRecipe(d).then((res) => {
        if (res.code == 200) {
          this.chufangData1 = res.data;
          console.log(res.data,'0000')
          this.$refs.rightYZ.activeName = "one";
          this.chufangData2 = [];
        }
      });
    },
    getClinic_doctorGetRecipeItem(id, type) {
      console.log("order_tyte", this.order_tyte);
      let d = { id: id };
      this.YZ_chufang_info_loading = true;
      this["_API"].getClinic_doctorGetRecipeItem(d).then((res) => {
        this.YZ_chufang_info_loading = false;
        if (res.code == 200) {
          this.chufangData2 = res.data;

          this.chufangData2.drug.forEach((res) => {
            if (res.is_skin_test === 0) {
              res.is_skin_test = false;
            } else {
              res.is_skin_test = true;
            }
            res.usage = res.usage;
            res.each_dos = parseInt(res.each_dos);
            res.trans_speed = parseInt(res.trans_speed);
            res.usage_extra = parseInt(res.usage_extra);
          });

          this.chufangData2.service.forEach((res) => {
            if (res.is_fast === 0) {
              res.is_fast = false;
            } else {
              res.is_fast = true;
            }
          });

          if (type == "update") {
            this.$refs.updateChufang.cfListSub = this.chufangData2.drug.map(
              (v, i) => {
                return {
                  id: v.drug_id,
                  name: v.drug?v.drug.name:'',
                  specification: v.drug.specification
                    ? v.drug.specification
                    : "",
                  count: v.num,
                  base_unit: v.drug.base_unit ? v.drug.base_unit : "",
                  each_dos: v.each_dos ? v.each_dos : "",
                  usage: v.usage ? parseInt(v.usage) : "",
                  is_skin_test: v.is_skin_test ? v.is_skin_test : "",
                  trans_speed: v.trans_speed ? v.trans_speed : "",
                  remark: v.remark ? v.remark : "",
                  price: v.drug.price ? v.drug.price : "", 
                  usage_extra: v.usage_extra ? v.usage_extra : "",
                  fee_category: {
                    name: v.drug.fee_category.name,
                  },
                  used_frqu: v.each_dos ? v.each_dos : "",
                  dose: v.dose ? v.dose : "",
                  dose_unit: v.dose_unit ? v.dose_unit : "",
                };
              }
            );

          console.log(" this.$refs.updateChufang.cfListSub", this.$refs.updateChufang.cfListSub)
            this.$refs.updateChufang.zlListSub = this.chufangData2.service.map(
              (v, i) => {
                return {
                  id: v.service_id,
                  position: v.position,
                  remark: v.remark,
                  once: 1,
                  is_fast: v.is_fast,
                  name: v.service.name,
                  price: v.price,
                  execute_dept: {
                    name: v.service.execute_dept.name,
                  },
                  yb_code: v.service.yb_code,
                  yb_proportion: v.service.yb_proportion,
                  yb_type: v.service.yb_type,
                };
              }
            );
          }
        }
      });
    },
  },
  components: {
    ViewTitle,
    leftSearch,
    rightCZ,
    rightYZ,
    updateChufang,
    TopManu,
    Create,
  },
};
</script>

<style lang='scss' scoped>
@import "./DoctorWorkStation.scss";

.leftTitle {
  margin: 30px 0;
  display: flex;
  align-items: center;
  position: relative;
  > .leftTitle_but {
    margin-right: 10px;
  }
}

/deep/ .el-tabs__item.is-active {
  color: #02bf6b;
}

/deep/ .el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #02bf6b;
}

.fenye {
  text-align: right;
  margin-top: 20px;
}

.jiaohaoDialogSty {
  text-align: center;
  > div {
    line-height: 30px;
  }
  > .name {
    span {
      width: 100px;
    }
  }
}

/deep/ .el-dialog--center .el-dialog__body {
  padding: 25px 25px 1px;
  display: inline-block;
}

.update {
  /deep/ .el-dialog {
    width: 100%;
    margin-top: 0 !important;
  }

  /deep/ .el-dialog__body {
    height: 100vh;
    overflow: scroll;
  }
}

/deep/ .el-main {
  padding: 5px 20px;
}
</style>
